package neusoft.yuejian_11.activity;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.squareup.picasso.Picasso;

import neusoft.yuejian_11.base.BaseActivity;
import neusoft.yuejian_11.bean.AddPrize;
import neusoft.yuejian_11.bean.GetInfo;
import neusoft.yuejian_11.bean.IsAddPrize;
import neusoft.yuejian_11.bean.IsAttentionBean;
import neusoft.yuejian_11.bean.PrizeBean;
import neusoft.yuejian_11.listener.RetrofitListener;
import neusoft.yuejian_11.model.AttentionModel;
import neusoft.yuejian_11.model.UserModel;
import neusoft.yuejian_11.R;

import static neusoft.yuejian_11.interF.Const.BASE_URI;

//兑换奖品（已改）
public class AddPrizeActivity extends BaseActivity implements RetrofitListener {
   protected Context context;
    private int giftId;
   private TextView phone;
   private  TextView address;
    private TextView peoplename;
    private TextView prizename;
    private TextView prizeduihuanzhi;
   private ImageView imageView;
    private ImageButton imageButtonBack;
   private Button add;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_prize);
        initView();//控件初始化
        initEvents();//点击事件
        initData();
    }

    private void initData() {
        giftId = getIntent().getIntExtra("g_id",-1);
        UserModel userModel = new UserModel();
        userModel.PrizeByIdDetail(String.valueOf(giftId),this);
        UserModel userModel1 = new UserModel();
        userModel1.getUserInfo(getU_id(),this);


    }

    private void initEvents() {
        //添加兑奖信息
        add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                UserModel userModel = new UserModel();
                userModel.addPrize(getU_id(), String.valueOf(giftId),AddPrizeActivity.this);
            }
        });
        imageButtonBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

    }
    private void initView() {
        phone= (TextView) findViewById(R.id.text_user_phone);
        address= (TextView) findViewById(R.id.text_user_address);
        peoplename= (TextView) findViewById(R.id.text_user_name);
        prizeduihuanzhi= (TextView) findViewById(R.id.text_prize_duihaunzhi);
        prizename= (TextView) findViewById(R.id.giftname1);
        imageView= (ImageView) findViewById(R.id.imageView);
        imageButtonBack = (ImageButton) findViewById(R.id.imageButtom_back);
        add= (Button) findViewById(R.id.addprize);

    }

    @Override
    public void onSuccess(Object o, int flag) {
        if(flag==1) {
            PrizeBean prizeBean = (PrizeBean) o;
            prizename.setText(prizeBean.getG_name());
            prizeduihuanzhi.setText(prizeBean.getG_values());
            Picasso.with(context)
                    .load(BASE_URI+"giftpic/" + prizeBean.getG_pic())
                    .into(imageView);
        }
        if (flag==2){
                GetInfo getInfo = (GetInfo) o;
                peoplename.setText(getInfo.getU_name());
                phone.setText(getInfo.getU_phone());
                address.setText(getInfo.getU_address());

        }


        if (flag==3) {
            //添加兑奖信息
            AddPrize addPrize = (AddPrize) o;
            if (addPrize.getSuccess().equals("0")) {
                Toast.makeText(AddPrizeActivity.this, "兑奖失败", Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(AddPrizeActivity.this, "兑奖成功", Toast.LENGTH_SHORT).show();
            }
        }

    }

    @Override
    public void onFail() {

    }

}
